##Case Selection Demonstration
setwd("~/Dropbox/!!Papers/Liberal Peace/Nina Gartzke paper/2013-20/ReplicationFiles")

set.seed(1234)
n <- 300
Yl <- rep(NA, n)
e <- rep(NA, n)
S <- rep(NA, n)
C <- rep(NA, n)

s <- 80
e <- rnorm(n,0,2)
S <- rbinom(n,1,s/100)
Sl <- as.factor(S)
C <- runif(n,-0.5, 8.5)
C <- round(C)
Yl <- 3 -0.5*C * S + e 
hist(Yl)

cor(S,C)

#http://stat.ethz.ch/R-manual/R-devel/library/grDevices/html/plotmath.html
library("ggplot2")
graph <- function(p){
  ids <- c("War High C", "MID High C", "War Medium C")
  xp1 <- c(7.5,8.5,8.5,7.5)
  xp2 <- c(7.5,8.5,8.5,7.5)
  xp3 <- c(7.5,8.5,8.5,7.5)
  text.size <- 4
  p <- ggplot(NULL,aes(y=Yl, x=C, colour=Sl, shape=Sl)) + 
    geom_rect(aes(NULL, NULL), xmin=4.7, xmax=8.3, ymin=2.1, ymax=7.5, alpha=0, colour="blue") +
    geom_rect(aes(NULL, NULL), xmin=7.7, xmax=8.3, ymin=0.1, ymax=1.9, alpha=0, colour="green") +
    geom_rect(aes(NULL, NULL), xmin=7.7, xmax=8.3, ymin=-2.5, ymax=-0.1, alpha=0, colour="purple") +
    annotate("text", x = 8.1, y = 7.3, label = "R1", size = text.size, hjust=0) +
    annotate("text", x = 8.1, y = 1.7, label = "R2", size = text.size, hjust=0) +
    annotate("text", x = 8.1, y = -0.3, label = "R3", size = text.size, hjust=0) +
    ylab(expression(paste(italic(Y), ":  Severity of Conflict")))+
    xlab(expression(paste(italic(C), ":  Capital Openness")))+
    ylim(-5, 7.5) +
    
   scale_colour_manual(name="Scope Conditions", labels=c("Absent", "Present"), values=c("red3", "blue"))+
    #scale_colour_discrete(name="Scope Conditions", labels=c("Absent", "Present"), h.start=330, c=100) +
    #scale_colour_brewer(palette="Set1") +
    scale_shape_manual(values=c(19,4), name="Scope Conditions", labels=c("Absent", "Present")) +
    geom_point(aes(y=Yl, x=C, colour=Sl, shape=Sl), size=5) + 
    geom_hline(aes(yintercept=c(5,2,0)), linetype="dashed") +
    annotate("text", x = -1.13, y = 5.3, label = "War", size = text.size, hjust=0) +
    annotate("text", x = -1.13, y = 2.3, label = "Fatal MID", size = text.size, hjust=0) +
    annotate("text", x = -1.13, y = 0.3, label = "MID", size = text.size, hjust=0) +
    theme_bw() + 
    theme(text = element_text(size=18)) +
    ggtitle(bquote(paste("Scope Conditions Present for ", .(s), "% of Cases")))
    #ggtitle(expression(paste("Scope Conditions Absent for ", s, "% of Cases"))) 
  
  p
  return(p)
}
#ylab(bquote(.(Y^*) Severity of Conflict)) +
#ylab(expression(paste(Y^*,"Severity of Conflict")), sep=" ") +
#scale_shape_discrete(name="Scope Conditions", labels=c("Absent", "Present")) +

seed <- 34280928
  #154842
#seed selected so that there are always some cases missing scope conditions in Category 1

e <- rnorm(n,0,2)
C <- runif(n,-0.5, 8.5)
C <- round(C)

s <- 95
set.seed(seed)
S <- rbinom(n,1,s/100)
Sl <- as.factor(S)
Yl <- 3 -0.5*C * S + e 
graph(p2)
base=7
ggsave("Figures/fig95.pdf", width=2*base, height=base)

s <- 90
set.seed(seed)
S <- rbinom(n,1,s/100)
Sl <- as.factor(S)
Yl <- 3 -0.5*C * S + e 
hist(Yl)

graph(p2)
ggsave("Figures/fig90.pdf", width=2*base, height=base)

s <- 80
set.seed(seed)
S <- rbinom(n,1,s/100)
Sl <- as.factor(S)
Yl <- 3 -0.5*C * S + e 
hist(Yl)

graph(p2)
ggsave("Figures/fig80.pdf", width=2*base, height=base)

s <- 70
set.seed(seed)
S <- rbinom(n,1,s/100)
Sl <- as.factor(S)
Yl <- 3 -0.5*C * S + e 
hist(Yl)

graph(p2)
ggsave("Figures/fig70.pdf", width=2*base, height=base)

s <- 50
set.seed(seed)
S <- rbinom(n,1,s/100)
Sl <- as.factor(S)
Yl <- 3 -0.5*C * S + e 
hist(Yl)

graph(p2)
ggsave("Figures/fig50.pdf", width=2*base, height=base)

s <- 30
set.seed(seed)
S <- rbinom(n,1,s/100)
Sl <- as.factor(S)
Yl <- 3 -0.5*C * S + e 
hist(Yl)

graph(p2)
ggsave("Figures/fig30.pdf", width=2*base, height=base)

